<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" size="small">
      <el-form-item label-width="0">
        <el-button size="small" @click="handleAdd" class="btn-add">添加 </el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table
        ref="returnReasonTable"
        height="100%"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="{ row }">{{ row.id }}</template>
        </el-table-column>
        <el-table-column label="原因类型" minWidth="120" align="center">
          <template #default="{ row }">{{ row.name }}</template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="{ row }">{{ row.sort }}</template>
        </el-table-column>
        <el-table-column label="是否可用" width="100" align="center">
          <template #default="{ row }">
            <el-switch v-model="row.status" @change="handleStatusChange(row)" :active-value="1" :inactive-value="0"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" width="180" align="center">
          <template #default="{ row }">{{ formatDateTime(row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="handleUpdate(row)">编辑</el-button>
            <el-button link type="danger" size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option v-for="item in operateOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
      </el-select>
      <el-button class="search-button" @click="handleBatchOperate" type="primary" size="small"> 确定 </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="添加退货原因" v-model="dialogVisible" width="30%">
      <el-form :model="returnReason" ref="reasonForm" label-width="auto">
        <el-form-item label="原因类型：">
          <el-input v-model="returnReason.name" class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="returnReason.sort" class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-switch v-model="returnReason.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { fetchList, deleteReason, updateStatus, addReason, getReasonDetail, updateReason } from '@/api/returnReason'
import { formatDateTime } from '@/utils/format'
import type { ResourceDto } from '@/models/resource'
import type { PageDto } from '@/models/request'

defineOptions({ name: 'ReasonList' })
const list = ref<ResourceDto[]>([])
const total = ref(0)
const multipleSelection = ref<ResourceDto[]>([])
const listLoading = ref(true)
const listQuery = ref<PageDto>({ pageNum: 1, pageSize: 10 })
const operateType = ref(null)
const dialogVisible = ref(false)
const returnReason = ref<ResourceDto>({})
const operateReasonId = ref()
const operateOptions = ref([{ label: '删除', value: 1 }])
getList()
function handleAdd() {
  dialogVisible.value = true
  operateReasonId.value = null
  returnReason.value = {}
}
async function handleConfirm() {
  if (operateReasonId.value == null) {
    //添加操作
    await addReason(returnReason.value)
    ElMessage.success('添加成功！')
  } else {
    //编辑操作
    await updateReason(operateReasonId.value, returnReason.value)
    ElMessage.success('修改成功！')
  }
  dialogVisible.value = false
  operateReasonId.value = null
  getList()
}
async function handleUpdate(row: ResourceDto) {
  dialogVisible.value = true
  operateReasonId.value = row.id
  const response = await getReasonDetail(row.id as number)
  returnReason.value = response.data.data
}
function handleDelete(row: ResourceDto) {
  rowDel(row.id + '')
}
function handleSelectionChange(val: ResourceDto[]) {
  multipleSelection.value = val
}
async function handleStatusChange(row: ResourceDto) {
  let param = { status: row.status, ids: row.id + '' }
  await updateStatus(param)
  ElMessage.success('修改成功')
}
function handleBatchOperate() {
  if (multipleSelection.value == null || multipleSelection.value.length < 1) {
    ElMessage.success('请选择要操作的条目')
    return
  }
  if (operateType.value === 1) {
    const ids = multipleSelection.value.map((item) => item?.id).join(',')
    //批量删除
    rowDel(ids)
  }
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
async function rowDel(ids: string) {
  await ElMessageBox.confirm('是否要进行该删除操作?', '提示', { type: 'warning' })
  await deleteReason({ ids })
  ElMessage.success('删除成功！')
  listQuery.value.pageNum = 1
  getList()
}
</script>
<style scoped>
.input-width {
  width: 80%;
}
</style>
